import random

from django.http import HttpResponse


# Create your views here.

from django_redis import get_redis_connection
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from meiduo.libs.captcha.captcha import captcha
from meiduo.utils.yuntongxun.sms import CCP
from verifications import constants
from verifications.serializer import ImageCodeCheckSerializer
from celery_tasks.sms.tasks import send_sms_code

import logging


logger = logging.getLogger("django")

class ImageCodeView(APIView):
    # def get(self,request,image_code_id):
    #     text,image_code = captcha.generate_captcha()
    #     redis_conn = get_redis_connection("verify_codes")
    #     redis_conn.setex("img_%s" % image_code_id,constants.IMAGE_CODE_REDIS_EXPIPES,text)
    #     return HttpResponse(image_code,content_type="image/jpg")
    def get(self,request,image_code_id):

        #生成验证码图案
        text,image = captcha.generate_captcha()
        #保存真实值
        redis_conn = get_redis_connection("verify_codes")
        print(text)

        redis_conn.setex("img_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIPES, text)

        #返回图片
        return HttpResponse(image,content_type="image/jpg")


class SMSCodeView(GenericAPIView):

    serializer_class = ImageCodeCheckSerializer

    def get(self,request,mobile):
        serlializer = self.get_serializer(data=request.query_params)
        serlializer.is_valid(raise_exception=True)

        #短信验证码
        sms_code = "%06d"%random.randint(0,999999)
        print(sms_code)
        redis_conn = get_redis_connection("verify_codes")
        pl = redis_conn.pipeline()
        pl.setex("sms_%s" % mobile,constants.SMS_CODE_REDIS_EXPIRES,sms_code)
        pl.setex("send_flag_%s" % mobile,constants.SEND_SMS_CDDE_INTERVAL,1)
        pl.execute()


        temp_id =constants.SMS_CODE_TEMP_ID
        expires = constants.SMS_CODE_REDIS_EXPIRES
        send_sms_code.delay(mobile,sms_code,expires,temp_id)

        return Response({"message":"ok"})


    # serializer_class = ImageCodeCheckSerializer
    #
    # def get(self,request,mobile):
    #
    #     #校验参数 序列化器
    #     serlializer = self.get_serializer(data = request.query_params)
    #     serlializer.is_valid(raise_exception=True)
    #     #短信验证码
    #     sms_code = "%06d"%random.randint(0,999999)
    #     #保存验证码，发送记录
    #     redis_conn = get_redis_connection("verify_codes")
    #     # redis_conn.setex("sms_%"%mobile,constants.SMS_CODE_EXPIRE,sms)
    #     # redis_conn.setex("send_flag_%s"%mobile,constants.SEND_SMS_CODE)
    #     pl = redis_conn.pipeline()
    #     pl.setex("sms_%"%mobile,constants.SMS_CODE_EXPIRE,sms)
    #     pl.setex("send_flag_%s"%mobile,constants.SEND_SMS_CODE_INTERVAL,1)
    #
    #     #管道通知redis执行命令
    #
    #     pl.execute()

    #     # 发送短信
    #     try:
    #         ccp=CCP()
    #         expires = constants.SMS_CODE_REDIS_EXPIRES
    #         result = ccp.send_template_sms(mobile,[sms_code,expires],constants.SMS_CODE_TEMP_ID)
    #
    #
    #     except Exception as e:
    #         logger.info("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e))
    #         return Response({'message': 'failed'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    #
    #     else:
    #         if result == 0:
    #             logging.info("发送验证码短信[正常][ mobile: %s ]" % mobile)
    #             return Response({'message': 'OK'})
    #
    #         else:
    #             logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)
    #             return Response({'message': 'failed'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    #





